package test.adddata;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-5-17 14:42
 */
public class 合并数据网页年数据合并到泰医主表20220518 {
    @SneakyThrows
    public static void main(String[] args) {

        //2022年5月18日09:56:35
        // 用于合并网页时间数据


        String dbNameMy = "spider_pubmed_vip_extend";
        String from = "gcr_pm_pub_year";

        int breakSize = 100000;

        SmallTool.printMemory();
        Db use3307 = Db.use();
        String allDate = "select max(pmid) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));

        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始页码++" + i + "++id++" + start + "结束id++++" + end);
            String meSql = "select id,pmid,html_year from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryMe = use3307.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer id = pmDeatil.getInt("id");
                Integer pmid = pmDeatil.getInt("pmid");
                String HTML_year = pmDeatil.getStr("html_year");
                if (HTML_year == null) {
                    Entity where = new Entity();
                    where.setTableName(dbNameMy);
                    where.set("id", id);
                    Entity exit = getExit(use3307, from, pmid);
                    if (exit != null) {
                        pmDeatil.set("html_year", exit.getStr("year"));
                        try {
                            use3307.update(pmDeatil, where);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }

            });

        }


    }

    @SneakyThrows
    public static Entity getExit(Db use, String table, Integer pmid) {
        Entity entity = use.queryOne("select * from " + table + " where pmid = ?", pmid);
        return entity;

    }


}

